<template>
    <v-card>
        <v-card-title>
            <v-btn color="primary" @click="addBrand">新增品牌</v-btn>
            <!--搜索框，与search属性关联-->
            <v-spacer/>
            <v-flex xs3>
                <v-text-field label="输入关键字搜索" v-model.lazy="search" append-icon="search" hide-details/>
            </v-flex>
        </v-card-title>
        <v-divider/>
        <v-data-table
                :headers="headers"
                :items="brands"
                :pagination.sync="pagination"
                :total-items="totalBrands"
                :loading="loading"
                :dark="dark"
                class="elevation-1"
        >
            <template slot="items" slot-scope="props">
                <td class="text-xs-center">{{ props.item.id }}</td>
                <td class="text-xs-center">{{ props.item.name }}</td>
                <td class="text-xs-center">
                    <img v-if="props.item.image" :src="props.item.image" width="130" height="40">
                    <span v-else>img...</span>
                </td>

                <td class="text-xs-center">{{ props.item.letter }}</td>

                <td class="justify-center layout px-0">
                    <v-btn color="info" @click="editBrand(props.item)">编辑</v-btn>
                    <v-btn color="warning" @click="deleteBrand(props.item.id)">删除</v-btn>
                </td>
            </template>
            <!-- <template @slot:pageText="props">
                 当前 {{ (this.pagination.page-1)*(this.pagination.rowsPerPage) }} - {{ (this.pagination.page-1)*(this.pagination.rowsPerPage)+(this.pagination.rowsPerPage-1) }} 共 {{ this.totalBrands}}条
             </template>-->
        </v-data-table>
        <!--弹出的对话框-->
        <v-dialog max-width="500" v-model="show" persistent scrollable>
            <brand-form @close="closeWindow" :oldBrand="oldBrand" :isEdit="isEdit"

            />
        </v-dialog>
    </v-card>
</template>

<script>
    import BrandForm from './BrandForm'

    export default {
        name: "my-brand",
        data() {
            return {
                search: "",
                totalBrands: 0, // 总条数
                brands: [], // 当前页品牌数据
                loading: true, // 是否在加载中
                pagination: {},
                dark: true, // 分页信息
                headers: [
                    {text: 'ID', align: 'center', value: 'id'},
                    {text: '名称', align: 'center', sortable: false, value: 'name'},
                    {text: 'LOGO', align: 'center', sortable: false, value: 'image'},
                    {text: '首字母', align: 'center', value: 'letter', sortable: true,},
                    {text: '操作', align: 'center', value: 'id', sortable: false}
                ],
                show: false,// 控制对话框的显示
                oldBrand: {}, // 即将被编辑的品牌数据
                isEdit: false, // 是否是编辑
            }
        },
        watch: {
            pagination: { //监视pagination属性的变化
                deep: true, //deep为true会监视pagination的属性及属性中对象属性的变化
                handler() {
                    //变化后的回调函数
                    this.getDataFromServer();
                }
            },
            search: {
                handler() {
                    this.getDataFromServer();
                }
            }
        },

        mounted() { // 渲染后执行
            // 查询数据
            this.getDataFromServer();
        },

        methods: {
            getDataFromServer() { // 从服务的加载数据的方法。
                // 开启加载
                this.loading = true;
                this.$http.get("/item/brand/page", {
                    params: {
                        keyWord: this.search,
                        currentPage: this.pagination.page,
                        sortBy: this.pagination.sortBy,
                        desc: this.pagination.desc,
                        pageSize: this.pagination.rowsPerPage
                    }
                }).then(resp => {
                    this.brands = resp.data.items,
                        this.totalBrands = resp.data.total,
                        this.loading = false
                })
            },
            addBrand() {
                // 修改标记
                this.isEdit = false;
                // 控制弹窗可见：
                this.show = true;
                // 把oldBrand变为null
                this.oldBrand = null;
            },

            editBrand(oldBrand) {
                // 根据品牌信息查询商品分类
                this.$http.get("/item/category/of/brand/?id=" + oldBrand.id)
                    .then(({data}) => {
                        // 修改标记
                        this.isEdit = true;
                        // 控制弹窗可见：
                        this.show = true;
                        // 获取要编辑的brand
                        this.oldBrand = oldBrand;
                        // 回显商品分类
                        this.oldBrand.categories = data;
                    })
            },
            deleteBrand(bid) {
                this.$message.confirm("确定删除吗？").then(() => {
                    this.$http.delete("/item/brand/delete/" + bid).then(() => {
                        this.getDataFromServer();
                    }).catch(() => {
                        this.$message.error("服务器异常删除失败");
                    });
                }).catch(() => {
                    this.$message.info("操作取消");
                })
            },
            closeWindow() {
                // 重新加载数据
                this.getDataFromServer();
                // 关闭窗口
                this.show = false;
            }
        },
        components: {
            BrandForm
        }
    }
</script>

<!-- scoped:当前样式只作用于当前组件的节点 -->
<style scoped>

</style>